package com.easyedu.dean.controller;

import com.easyedu.core.entity.po.Course;
import com.easyedu.core.entity.po.OpenCourse;
import com.easyedu.core.entity.po.StudentSelect;
import com.easyedu.core.util.R;
import com.easyedu.dean.entity.dto.request.ReqApprovalEffective;
import com.easyedu.dean.entity.dto.request.ReqCourse;
import com.easyedu.core.entity.query.CourseQuery;
import com.easyedu.dean.service.CourseService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lmr</ br>
 * 开设课程管理
 **/
@RestController
@RequestMapping("/dean")
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 功能描述:获取指定课程
     *
     * @param openInstituteName 1 开课学院名称
     * @param courseName        2 课程名称
     * @param courseCode        3 课程代码
     * @param courseNature      4 课程性质
     * @param examinationMethod 5 考核方式
     * @param gradeType         6 成绩类型
     * @return : com.easyedu.api.util.R
     **/
    @GetMapping("/course")
    public R getCourse(@RequestParam(required = false) String openInstituteName,
                       @RequestParam(required = false) String courseName,
                       @RequestParam(required = false) String courseCode,
                       @RequestParam(defaultValue = "0") long courseNature,
                       @RequestParam(defaultValue = "0") long examinationMethod,
                       @RequestParam(defaultValue = "0") long gradeType) {
        CourseQuery course = new CourseQuery();
        course.setOpenInstituteName(openInstituteName);
        course.setCourseName(courseName);
        course.setCourseCode(courseCode);
        course.setCourseNature(courseNature);
        course.setExaminationMethod(examinationMethod);
        course.setGradeType(gradeType);
        List<Course> courseList = courseService.getCourse(course);
        if (courseList.size() == 0) {
            return R.error().message("没有查到符合条件的课程");
        }
        return R.ok().data(courseList);
    }

    /**
     * 功能描述: 创建课程
     *
     * @param reqCourse 1 课程
     * @return : com.easyedu.api.util.R
     **/
    @PostMapping("/course")
    public R createCourse(@RequestBody @Valid ReqCourse reqCourse) {
        long courseId = courseService.createCourse(reqCourse);
        if (courseId == 0) {
            return R.error().message("插入课程失败");
        }
        return R.ok().message("插入课程成功").data("新增课程id为", courseId);
    }

    /**
     * 功能描述:获取待审批考试课程
     *
     * @return : com.easyedu.api.util.R
     **/
    @GetMapping("/course/approval")
    public R approvalCourse() {
        List<OpenCourse> openCourses = courseService.getApprovalCourse();
        return R.ok().data(openCourses);
    }

    /**
     * 功能描述: 获取课程成绩
     *
     * @param openCoursesId 1 开放课程唯一id
     * @return : com.easyedu.api.util.R
     **/
    @GetMapping("/course/grade")
    public R getCourseGrade(@RequestParam Long openCoursesId) {
        List<StudentSelect> courseGrade = courseService.getCourseGrade(openCoursesId);
        return R.ok().data(courseGrade);
    }

    /**
     * 功能描述:审批通过课程成绩
     *
     * @param reqApprovalEffective 1 审批通过数据
     * @return : com.easyedu.api.util.R
     **/
    @PostMapping("/course/approval")
    public R passApprovedCourse(@RequestBody @Valid ReqApprovalEffective reqApprovalEffective) {
        String s = courseService.passApprovedCourse(reqApprovalEffective.getOpenCoursesId());
        return R.ok().message(s);
    }
}
